package com.itheima.d8_thread_pool;

import java.util.concurrent.*;

/*
     目标：线程池处理任务
     需求 : 使用线程池处理Callable类型任务
 */
public class ThreadPoolTest3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor exe = new ThreadPoolExecutor(
                3,
                10,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(20),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

        Future<Integer> fu1 = exe.submit(new Cal(10));
        Future<Integer> fu2 = exe.submit(new Cal(20));
        Future<Integer> fu3 = exe.submit(new Cal(50));


        System.out.println(fu1.get());
        System.out.println(fu2.get());
        System.out.println(fu3.get());
    }




}

class Cal implements Callable<Integer>{

    private int num;
    public Cal(int num){
        this.num = num;
    }
    int sum = 0;
    @Override
    public Integer call() throws Exception {
        for (int i = 1; i <= num; i++) {
            sum += i;
        }
        return sum;
    }

}
